package sg.edu.nus.iss.vmcs.customer;

import java.awt.*;

import sg.edu.nus.iss.vmcs.store.Coin;
import sg.edu.nus.iss.vmcs.store.StoreItem;

/**
* This class represents the object interface that is the part of the Customer
* Panel. It is used to enter Coins into VMCS. This class inherits from the
* Java AWT Panel Class.
* 
* @version 3.0 29/11/2011
* @author  Team 7S - SE19 
*/

public class CoinInputBox extends Panel {

/**
 * Active Color of type Color.
 */	
	public final static Color ACT_COLOR = Color.white;

	private double[] weights;
	private CoinReceiver coinReceive;
	private Button[] bn;
	private int i = 0;

/**
 * This constructor creates an instance of CoinImputBox. It receives
 * the reference of Container and Controller CoinReceiver as parameters.
 * @param composite Container for components
 * @param coinRcv CoinReceiver
 * @see CoinReceiver
 */	
	public CoinInputBox(Container composite, CoinReceiver coinRcv) {

		coinReceive = coinRcv;
		this.setLayout(new BorderLayout());

		Label lb = new Label("Enter Coins Here");
		lb.setFont(new Font("Helvetica", Font.BOLD, 12));
		lb.setAlignment(Label.LEFT);
		Panel lp = new Panel(new GridLayout(1,1));
		lp.add(lb);
		
		StoreItem[] items = coinReceive.getCashStore ();
		Panel tp = new Panel(new GridLayout(1, items.length+1));
		weights = new double[items.length+1];
		bn      = new Button[items.length+1];
		for (i = 0; i < items.length; i++) {
			Coin ob = (Coin)items[i].getContent();
			weights[i] = ob.getWeight();
			bn[i] = new Button(ob.getName());
			bn[i].setBackground(ACT_COLOR);
			bn[i].setFont(new Font("Helvetica", Font.BOLD, 14));
			tp.add(bn[i]);
		}
		bn[i] = new Button("Invalid");
		weights[i] = 0;
		bn[i].setFont(new Font("Helvetica", Font.BOLD, 14));
		bn[i].setBackground(ACT_COLOR);
		tp.add(bn[i]);
		this.add("North",  lp);
		this.add("Center", tp);
	}

/**
 * This operation attach listener and set action command to Button objects.
 */	
	public void addListener() {
		for (i = 0; i < bn.length  ; i++) {
			bn[i].addActionListener(new CoinInputListener(coinReceive));
			bn[i].setActionCommand(String.valueOf(weights[i]));
		}
	}

/**
 * This operation activates the Coin Input Box if the parameter is TRUE. Otherwise
 * the Coin Input Box is deactivated.
 * @param active boolean state either TRUE or FALSE
 */	
	public void setActive(boolean active){
		for(Button btn : bn){
			btn.setEnabled(active);
		}
	}
}
